package com.jbm.jbmsupplier.presenters;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.jbm.jbmsupplier.modes.SplsInterface;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.jbmsupplier.view.DoubleButtonDialog;
import com.jbm.jbmsupplier.views.SplsViews;
import com.jbm.jbmsupplier.zp.service.MyService;
import com.jbm.onehome.iface.IAppVersion;

/**
 * Created by 家帮忙 on 2017/7/6.
 */

public class SplasActivitys implements SplsInterface {
    private SplsViews splsViews;
    private Context context;
    private String url;
    private SharedPreferActivity sharedLoginActivity;
    private MyConnSock mycon = null;
    private IntentFilter filer;
    private final int VISITION = 0001;
private int i=0;
    public SplasActivitys(SplsViews splsViews, Context context) {
        this.splsViews = splsViews;
        this.context = context;
    }
    public void setInitIal() {//初始化数据
        setInit();
    }

    @Override
    public void setInit() {
        sharedLoginActivity = new SharedPreferActivity(context);
        context.startService(new Intent(context, MyService.class));
        mycon = new MyConnSock();
        filer = new IntentFilter();
        filer.addAction("SOCKCONN");
        if (sharedLoginActivity.getBoolearNetConn() == false) {
            context.registerReceiver(mycon, filer);
        } else {
            context.registerReceiver(mycon, filer);
        }
    }

    public void setSplaVison(String vison) {//获取版本号
        setVision(vison);
    }

    String maxs, mins;

    @Override
    public void setVision(String vision) {//將版本号分割传到服务器
        maxs = vision.substring(0, vision.indexOf("."));
        mins = vision.substring(vision.indexOf(".") + 1, vision.length());
        Thread thread = new Thread(runnable);
        thread.start();
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            NetIntent netIntent = new NetIntent(handler);
            netIntent.setDetection(Integer.parseInt(maxs), Integer.parseInt(mins), VISITION);
        }
    };

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case VISITION:
                    final IAppVersion invision = (IAppVersion) msg.obj;
                    if (invision.m_code==200) {
                      if (  !invision.app_url.isEmpty()) {
                            url = invision.app_url;
                            final DoubleButtonDialog dialog = new DoubleButtonDialog(context, invision.errorinfo);
                            dialog.setNoText("下次更新");
                            dialog.setTetle_message("版本更新...");
                            dialog.setisVisibilitymess();
                            dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                        splsViews.setSucc(url, sharedLoginActivity.getBoolearLogin());

                                    dialog.dismiss();
                                }
                            });
                            dialog.getnobtn().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                     if (invision.milestone > 0) {
                                       splsViews.setfinish();
                                     } else {
                                    splsViews.setSucc(url, sharedLoginActivity.getBoolearLogin());
                                     }
                                    dialog.dismiss();
                                }
                            });
                        }else{
                          splsViews.setError("", sharedLoginActivity.getBoolearLogin());
                      }
                    } else if (invision.m_info.equals("Not Connect")) {
                        if (i==0) {
                            splsViews.setError("", sharedLoginActivity.getBoolearLogin());
                            i=1;
                        }


                    } else if (invision.m_code == 500) {
                        splsViews.setError("", sharedLoginActivity.getBoolearLogin());
                    } else {
                        splsViews.setError("", sharedLoginActivity.getBoolearLogin());
                    }
                    break;
                default:
            }
        }
    };

    class MyConnSock extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent)

        {
            if (intent.getStringExtra("comm").equals("2")) {
                Toast.makeText(context, "连接失败请检查网络", Toast.LENGTH_SHORT).show();
                splsViews.setInvisionit(); //检测版本号
            } else if (intent.getStringExtra("comm").equals("1")) {
                splsViews.setInvisionit(); //检测版本号
            } else if (intent.getStringExtra("comm").equals("3")) {
                splsViews.setInvisionit(); //检测版本号
            }
        }
    }

    public void setonDestroy() {//销毁当前页面
        if (sharedLoginActivity.getBoolearNetConn() == false) {
            if (mycon != null) {
                context.unregisterReceiver(mycon);
            }
            return;
        } else {
            if (mycon != null) {
                context.unregisterReceiver(mycon);
            }
            return;
        }
    }
}
